import {wxPay,wechatGetInfo} from "@/api/api.js";
import common from "@/common/common";
import { request } from "../api/config";
module.exports={
    // /* 判断是否在公众号里面 */
    isWechat:function(){
        var ua = window.navigator.userAgent.toLowerCase();  
        if(ua.match(/micromessenger/i) == 'micromessenger'){  
            return true;  
        }else{  
            return false;  
        }  
    },
    jsApiCall(data ,callback_succ_func ,callback_error_func){  
		//使用原生的，避免初始化appid问题  
		WeixinJSBridge.invoke('getBrandWCPayRequest', {  
			appId:data.appId,
			timeStamp: String(data.timeStamp),  
			nonceStr: data.nonceStr, // 支付签名随机串，不长于 32 位  
			package: data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）  
			signType: data.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'  
			paySign: data.sign, // 支付签名  
		},  
		function(res) {  
			var msg = res.err_msg ?res.err_msg :res.errMsg;  
			//WeixinJSBridge.log(msg);  
			switch (msg) {  
				case 'get_brand_wcpay_request:ok': //支付成功时  
					if(callback_succ_func){  
                        callback_succ_func(res); 
					}  
					break;  
				default: //支付失败时  
					WeixinJSBridge.log('支付失败!'+msg+',请返回重试.');  
					if(callback_error_func){  
						callback_error_func({msg:msg});  
					}  
					break;  
			}  
		})  
	},
    // appid:wxb0921e9575b2efbb,
    
    /*JSAPI支付*/ 
    jsApiPayment(data ,callback_succ_func ,callback_error_func){
        if (typeof WeixinJSBridge == "undefined"){
            if (document.addEventListener) {  
				document.addEventListener('WeixinJSBridgeReady', this.jsApiCall, false);  
			} else if (document.attachEvent) {  
				document.attachEvent('WeixinJSBridgeReady', this.jsApiCall);  
				document.attachEvent('onWeixinJSBridgeReady', this.jsApiCall);  
			}  
         }else{
            var that =this;
            // is_mini_app true 小程序 false 公众号
            wxPay({trade_type:"JSAPI",order_sn:data.order_sn,openid:data.openId, is_mini_app:false   }).then(res=>{
                console.log(res)
                if(res.status==1){
                    if(res.status==1) {
                        that.jsApiCall(res.result ,callback_succ_func ,callback_error_func);  
                    }
                }else{
					common.toast('none',res.msg,2000);
				}
                /*
                appId: "wxb0921e9575b2efbb"
                nonceStr: "EGz0Pg5XmGkTdeqr"
                package: "prepay_id=wx27210103533425c73a3eb0ce1873383400"
                partner_id: "1600461907"
                prepay_id: "wx27210103533425c73a3eb0ce1873383400"
                sign: "848B4C0548A895FC26146FC319A52DC3"
                signType: "MD5"
                timeStamp: "1595854863"
                */ 
            })
            // onBridgeReady();
         }
    },
    
}